<!-- add by lgx 20161110 
    提示： 最新版本列表弹窗功能，支持单选和多选，
    请不要使用 旧版本dialoggridselect.tag和dialoggridselects.tag
 -->
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>

<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>
<%@ attribute name="name" type="java.lang.String" required="true" description="隐藏域名称（ID）"%>
<%@ attribute name="value" type="java.lang.String" required="true" description="隐藏域值（ID）"%>
<%@ attribute name="labelName" type="java.lang.String" required="true" description="输入框名称（Name）"%>
<%@ attribute name="labelValue" type="java.lang.String" required="true" description="输入框值（Name）"%>
<%@ attribute name="tableSelectId" type="java.lang.String" required="true" description="弹出table框选择td后获取该对象的值（隐藏域）" %>
<%@ attribute name="tableSelectName" type="java.lang.String" required="true" description="弹出table框选择td后获取该对象的显示值" %>

<%@ attribute name="title" type="java.lang.String" required="true" description="选择框标题"%>
<%@ attribute name="includeUrl" type="java.lang.String" required="true" description="table列表路径"%>
<%@ attribute name="params" type="java.lang.String" required="false" description="参数集合，例如id=11&name=11(暂时不支持中文做参数)"%>
<%@ attribute name="singleSelect" type="java.lang.Boolean" required="false" description="设置True 将禁止多选" %>
<%@ attribute name="allowClear" type="java.lang.Boolean" required="false" description="是否允许清除"%>
<%@ attribute name="allowInput" type="java.lang.Boolean" required="false" description="文本框可填写"%>
<%@ attribute name="cssClass" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="cssStyle" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="smallBtn" type="java.lang.Boolean" required="false" description="缩小按钮显示"%>
<%@ attribute name="hideBtn" type="java.lang.Boolean" required="false" description="是否显示按钮"%>
<%@ attribute name="createBtn" type="java.lang.String" required="false" description="新增按钮"%>
<%@ attribute name="disabled" type="java.lang.String" required="false"
	description="是否限制选择，如果限制，设置为disabled"%>
<%@ attribute name="width" type="java.lang.String" required="true" description="窗口宽度"%>
<%@ attribute name="height" type="java.lang.String" required="true" description="窗口高度"%>
<%@ attribute name="draggable" type="java.lang.String" required="false"  description="是否可以拖动窗口,默认false，不允许拖地"%>

<!-- add for lxh 20160702 弹出框点击确定后执行的js函数执行函数（父页面传递过来），通过该函数可以实现操作父页面（例如根据子页面的值异步刷新父页面） -->
<%@ attribute name="completeJs" type="java.lang.String" required="false" description="弹出框后点击确定后执行的js函数"%>
<!-- add for lxh 20160702 弹出框点击之前触发函数-->
<%@ attribute name="beforeJs" type="java.lang.String" required="false" description="弹出框点击时候触发js函数"%>

<!-- 在这里设置默认值 -->
<c:if test="${empty draggable}">
<c:set  var="draggable" value="false" />
</c:if>
<c:if test="${empty singleSelect}">
<c:set  var="singleSelect" value="false" />
</c:if>


<div class="input-append">
    <input id="${id}params" name="${id}params"  type="hidden" value="${params}" />
    <input id="${id}singleSelect" name="${id}singleSelect"  type="hidden" value="${singleSelect}" />
    <input id="${id}rowData" name="${id}rowData"  type="hidden"/>
	<input id="${id}url" name="${id}url"  type="hidden" value="${includeUrl}" />
	<input id="${id}Id" name="${name}" class="${cssClass}" type="hidden" value="${value}" />
	<input id="${id}Name" name="${labelName}" ${allowInput?'':'readonly="readonly"'} type="text"
		value="${labelValue}" class="${cssClass}"
		style="${cssStyle}" />
	
	<a id="${id}Button" href="javascript:" class="btn btn-default btn-select ${allowClear ? 'btn-select-middle':''} ${disabled} ${hideBtn ? 'hide' : ''}">
		<i class="layui-icon layui-icon-search"></i>
	</a>
	<c:if test="${allowClear}">
        <style>
           #${id}Name{
			  border-radius:2px 0 0 2px;
		      width: 85%;
            }
        </style>
        <a id="${id}Button_clear" onclick="doClear('${id}');" href="javascript:" class="btn btn-default btn-select ${disabled} ${hideBtn ? 'hide' : ''}"><i class="layui-icon layui-icon-delete"></i></a>	
    </c:if>
	<!--<c:if test="${fn:contains(cssClass, 'required')}">
	   <span class="help-inline"><font color="red">*</font></span> 
	</c:if> -->
</div>


<script type="text/javascript">
	$("#${id}Button, #${id}Name").click(function(){
		
		//点击之后触发函数，可以预先初始化 父页面或者子页面 
		${beforeJs}
		
		// 是否限制选择，如果限制，设置为disabled
		if ($("#${id}Button").hasClass("disabled")){
			return true;
		}
		
		var includeUrl =$("#${id}url").val();
		var params =$("#${id}params").val();
		var singleSelect = $("#${id}singleSelect").val();
		
		var selectIds = $("#${id}Id").val();
		var selectNames = $("#${id}Name").val();
		
		// 正常打开	
		top.$.jBox.open("iframe:${ctx}/tag/dialogTableSelect?includeUrl="+encodeURIComponent(includeUrl)+"&params="+encodeURIComponent(params)+"&singleSelect="+singleSelect+"&tableSelectId=${tableSelectId}&tableSelectName=${tableSelectName}"+"&selectIds="+selectIds+"&selectNames="+encodeURIComponent(selectNames), "选择${title}", ${width}, ${height}, {
			top:"0%",
			persistent : true,
			draggable:${draggable},
			ajaxData:{selectIds: $("#${id}Id").val()},buttons:{${createBtn}"确定":"ok", ${allowClear?"\"清除\":\"clear\", ":""}"关闭":true}, submit:function(v, h, f){

				if (v=="ok"){
					
					var ids = [], names = [];
					
					var select_ids = h.find("iframe").contents().find(".select-id");
					var select_names = h.find("iframe").contents().find(".select-name");

					
					if (select_ids.length == 0){
						top.$.jBox.tip("请选择${title}。");
						return false;
					}
					
					$.each(select_ids,function (index,domEle){
						  ids.push($(domEle).val());
					});
					
					$.each(select_names,function (index,domEle){
						  names.push($(domEle).val());
					});
					
					
					$("#${id}Id").val(ids.join(",").replace(/u_/ig,""));
					$("#${id}Name").val(names.join(","));
					//只支持单选
					var singleSelect = '${singleSelect}';
					if(singleSelect == "true"){
						var row_data = h.find("iframe").contents().find(".select-row-data").val();   
						var row_data_String = JSON.stringify(row_data);
					
						row_data_String = row_data_String.replace(/\\/g,'');//去掉“\”
						row_data_String = row_data_String.substr(1);//去掉第一个"
						row_data_String = row_data_String.substr(0,row_data_String.length-1);//去掉最后一个"
						//alert(row_data_String);
						//alert(row_data_String.replace(/\\/g,''));//去掉“\”
						$("#${id}rowData").val(row_data_String);
					}
					
					${completeJs}
				}
				else if (v=="clear"){
					$("#${id}Id").val("");
					$("#${id}Name").val("");
					$("#${id}rowData").val("");
                }
				
				
			}, loaded:function(h){
				$(".jbox-content", top.document).css("overflow-y","hidden");
			}
		});
	});
	
	function doClear(id){
	    ${beforeJs}
	  	$("#"+id+"Id").val("");
		$("#"+id+"Name").val("");
		$("#"+id+"rowData").val("");
		${completeJs}
   }
</script>
